<?php
namespace app\controllers\api\account;

use fate\http\Request;
use app\models\User;
use app\libs\Uploader;
use app\libs\LibUser;
use app\libs\Sys;

class EditbannerController extends \app\controllers\ForceLoginPure {
    public function run() {
        $req = Request::getInstance();

        $uid = LibUser::getLoginUid();
        $banner = $req->getParameter('banner');

        if(!$banner) {
            $this->ajaxReturn(null, 300002, 'empty data');
            exit;
        }

        // 保存 banner 图
        $basePath = Sys::getBannerUploadPath($uid);
        $uploader = new Uploader();
        $uploader->setConfigs([
            'randomName' => false,
            'saveName' => (string)$uid,
            'rotatePattern' => '',
            'basePath' => './' . $basePath
        ]);
        $uploader->saveBase64Image($banner);
        $banner = $uploader->getFirstUploadedFile();

        if('' === $banner) {
            $this->ajaxReturn(null, 300003);
            exit;
        }

        // and prefix
        $banner = '/' . $basePath . $banner;

        $this->ajaxReturn([
            'banner' => $banner
        ]);
    }
}
